package se.schtohan.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import se.schtohan.model.Book;
import se.schtohan.repository.BookRepository;

import java.util.ArrayList;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    private BookRepository bookRepository;

    @Autowired
    public BookServiceImpl(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    public List<Book> getAllBooks() {
        Book book1 = new Book("1Q84", "Haruki Murakami", 1);
        Book book2 = new Book("Körkarlen", "Selma Lagerlöf", 2);
        List<Book> books = new ArrayList<Book>();
        books.add(book1);
        books.add(book2);
        return books;
    }

    @Transactional
    public void save(Book book) {
        bookRepository.save(book);
    }
}
